package mp.android.WLANWidget;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class WLANActivator extends Service {
	static final String TAG = "WLANActivator Svc"; 	
	
	boolean screenOn;
	
	boolean batteryLow;
	
	boolean wlanOn;
	
	WLANHandler wlanHandler;
	
	SharedPreferences prefs;
	
	WifiManager wifi;	

	BroadcastReceiver bcastScreenOff = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "Screen Off Event...");
			screenOn = false;
			// Start the handler if battery is ok and the user did not enable wifi
			if(!batteryLow && !wifi.isWifiEnabled() && prefs.getBoolean("enabled", false)){
				Log.d(TAG, "Start Wifi handler");
				
				// Did user change Wifi status?
				if(!wlanOn && wifi.isWifiEnabled()){
					// User turned Wifi on
					// don't mess around with settings
				}else if(wlanOn && !wifi.isWifiEnabled()){
					// User turned Wifi off
					wlanOn = false;
					// Schedule turn on wifi 
					wlanHandler.sendEmptyMessageDelayed(1, prefs.getInt("onTime", Settings.TIME_30m));
				}else{
					// Schedule turn on wifi 
					wlanHandler.sendEmptyMessageDelayed(1, prefs.getInt("onTime", Settings.TIME_30m));
				}
			}
		}
	};
	
	BroadcastReceiver bcastScreenOn =  new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "Screen On Event...");
			screenOn = true;
			// Clear all events from the handler when screen is 
			// turned on
			wlanHandler.removeMessages(1);
			// Keep turn off events --> may turn off wlan while 
			// user is operating the device...
			//wlanHandler.removeMessages(0);
			// Stop handler
			wlanHandler.sendEmptyMessage(-1);
		}
	};
	
	BroadcastReceiver bcastBatteryLow  = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "Battery Low Event ...");
			// Clear all events from the handler when battery is low
			wlanHandler.removeMessages(1);
			wlanHandler.removeMessages(0);
			// Stop handler
			wlanHandler.sendEmptyMessage(-1);
			// turn off wifi to save battery
			wifi.setWifiEnabled(false);
			wlanOn = false;
		}
	};
	
	BroadcastReceiver bcastBatteryFull  = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			batteryLow = false;
			// Start the handler if screen is off and the user did not enable wifi
			if(!screenOn && !wifi.isWifiEnabled() && prefs.getBoolean("enabled", false)){
				Log.d(TAG, "Start Wifi handler");
				wlanHandler.sendEmptyMessageDelayed(1, prefs.getInt("onTime", Settings.TIME_30m));
			}
		}
	};
	
	class WLANHandler extends Handler{		
		@Override
		public void handleMessage(Message msg) {
			if(msg.what != -1){
				Log.d(TAG, "handle message: " + msg.what);				
				if(wlanOn){
					wifi.setWifiEnabled(false);
					wlanOn = false;
					this.sendEmptyMessageDelayed(1, prefs.getInt("onTime", Settings.TIME_30m));
					Log.d(TAG, "Turned Off WLAN");
				}else{
					wifi.setWifiEnabled(true);
					wlanOn = true;
					this.sendEmptyMessageDelayed(0, prefs.getInt("offTime", Settings.TIME_3m));
					Log.d(TAG, "Turned On WLAN");
				}
			}
		}		
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(TAG, "startetd");
		prefs = getSharedPreferences(getPackageName(), MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		
		wlanHandler = new WLANHandler();		

		registerReceiver(bcastScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
		registerReceiver(bcastScreenOn, new IntentFilter(Intent.ACTION_SCREEN_ON));
		
		registerReceiver(bcastBatteryLow, new IntentFilter(Intent.ACTION_BATTERY_LOW));	
		registerReceiver(bcastBatteryFull, new IntentFilter(Intent.ACTION_BATTERY_OKAY));	
	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "destroyed");
		wlanHandler.removeMessages(1);
		wlanHandler.removeMessages(0);
		wlanHandler.sendEmptyMessage(-1);
		
		unregisterReceiver(bcastScreenOn);
		unregisterReceiver(bcastScreenOff);
		unregisterReceiver(bcastBatteryFull);
		unregisterReceiver(bcastBatteryLow);
	}	
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
}
